unit hadedataaccessor;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  hadedataset,
  hadeconnectionmanager,
  hadebasedataaccessor,
  hadebaseVisitor,
  hadecriteria,
  hademapbase;

type

  { THadeDataAccessor }

  THadeDataAccessor = class
  private
    FWithoutParams: boolean;
    IAccessor: IHadeDataAccessor;
    procedure setWithoutParams(AValue: boolean);
  public
    constructor Create(const AConnection: THadeConnection);

    procedure Read(const ATableName: string; const AObject: THadeVisited;
      const ACols, AOrderBy: TStringList; const whereClause: string = '';
      AOrderType: THadeOrderType = oASC; const limit: integer = 0;
      const offset: integer = 0);

    procedure Insert(const ATableName: string; const AObject: THadeVisited;
      ACols: THadeMapSimpleValue; const Execute: boolean = True);

    procedure Update(const ATableName: string; const AObject: THadeVisited;
      ACols: THadeMapSimpleValue; const whereClause: string;
      const Execute: boolean = True);

    procedure Delete(const ATableName, whereclause: string;
      const AObject: THadeVisited; const Execute: boolean = True);

    procedure ExecuteMultiple(Asql: string);

    property WithoutParams: boolean read FWithoutParams write setWithoutParams;
    function getDataSet: THadeDataSet;
  end;

implementation

uses
  hadebroker,
  hadeexception,
  hadeconst,
  hadesqldataaccessor;

{ THadeDataAccessor}

procedure THadeDataAccessor.setWithoutParams(AValue: boolean);
begin
  if FWithoutParams = AValue then
    Exit;
  FWithoutParams := AValue;
  IAccessor.withoutParams := fWithoutParams;
end;

constructor THadeDataAccessor.Create(const AConnection: THadeConnection);
begin
  case AConnection.Broker of
    SQLDB_SQLite: IAccessor := THadeSQLiteAccessor.Create(AConnection);
    SQLDB_Firebird: IAccessor := THadeFirebirdAccessor.Create(AConnection);
    else
      raise  EHadeBrokerCurrentlyNotSupported.CreateFmt(
        CHadeBrokerCurrentlyNotSupported, [BrokerAsString(AConnection.Broker)]);
  end;
  fWithoutParams := False;
  IAccessor.WithoutParams := fWithoutParams;
end;

procedure THadeDataAccessor.Read(const ATableName: string;
  const AObject: THadeVisited; const ACols, AOrderBy: TStringList;
  const whereClause: string; AOrderType: THadeOrderType; const limit: integer;
  const offset: integer);
begin
  IAccessor.Read(ATableName, AObject, ACols, AOrderBy, whereClause, AOrderType,
    limit, offset);
end;

procedure THadeDataAccessor.Insert(const ATableName: string;
  const AObject: THadeVisited; ACols: THadeMapSimpleValue; const Execute: boolean);
begin
  IAccessor.Insert(ATableName, AObject, ACols, Execute);
end;

procedure THadeDataAccessor.Update(const ATableName: string;
  const AObject: THadeVisited; ACols: THadeMapSimpleValue;
  const whereClause: string; const Execute: boolean);
begin
  IAccessor.Update(ATableName, AObject, ACols, whereClause, Execute);
end;

procedure THadeDataAccessor.Delete(const ATableName, whereclause: string;
  const AObject: THadeVisited; const Execute: boolean);
begin
  IAccessor.Delete(ATableName, whereClause, AObject, Execute);
end;

procedure THadeDataAccessor.ExecuteMultiple(Asql: string);
begin
  IAccessor.executeMultiple(ASQL);
end;

function THadeDataAccessor.getDataSet: THadeDataSet;
begin
  Result := IAccessor.getDataSet;
end;

end.

